<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Exception;//事务处理
use app\admin\model\Promodel;

class Bank extends Auth
{
    public function __construct(Request $request){
        $this->getChildIDdata = [];
        parent::__construct($request);
        $this->data = $request->param();
        $this->user_id = session('userid') == null ? 1 : session('userid');
        $this->lang = isset($this->data['lang']) ? $this->data['lang'] : is_lang();
    }

    // 配件
    public function list(Request $request)
    {   
        if($request->isAjax()){
            $language = getDatas('language',['*'],'1=1');
            // 获取分页数据
            $returnData['size'] = count( $language)+10;
            if(isset($this->data['page']) && isset($this->data['limit'])){
                if($this->data['limit']*($this->data['page']-1) < $returnData['size']){
                    $language = array_slice($language,$this->data['limit']*($this->data['page']-1),$this->data['limit']);
                    $returnData['page'] = $this->data['page'];;
                }
            }else{
                $returnData['page'] =1;
            }
            foreach ($language as $k => $v) {
                $language[$k]['is_hide'] = $v['hide'] == 1 ? getViewMultiLanguage('yes',$this->lang) : getViewMultiLanguage('no',$this->lang);
            }
            $returnData['language'] = $language;
            // dump($returnData);die;
            return $returnData;

        }else{
            return view('list',['request'=>'list']);
        }
        
    }

    public function addopen(){
        if(isset($this->data['request']) && $this->data['request'] == 'insert'){
            if(isset($this->data['name'])){
                if(isset($this->data['code'])){
                    if(isset($this->data['icon'])){
                        $has = hasData('language',['name'=>$this->data['name']]);
                        if($has){
                            return array('code'=>500,'msg'=>lang('has_data'));
                        }else{
                            unset($this->data['request']);
                            $res = insertData('language',$this->data);
                            if($res){
                                return array('code'=>2000,'msg'=>lang('success'));
                            }else{
                                return array('code'=>500,'msg'=>lang('insert_fail'));
                            }
                        }
                    }else{      
                        return array('code'=>500,'msg'=>lang('miss_icon'));
                    }
                }else{  
                    return array('code'=>500,'msg'=>lang('miss_code'));
                }
            }else{  
                return array('code'=>500,'msg'=>lang('miss_name'));
            }
        }else{
            return view('open',['title'=>'添加语言类型','request'=>'add']);
        }
    }


    public function editopen(){
        if(isset($this->data['request']) && $this->data['request'] == 'update'){

        // return $this->data;
            if(isset($this->data['id'])){
                unset($this->data['request']);
                $this->data['hide'] = $this->data['hide'] == 'true' ? 1 : 2;
                $res = updateData('language',['id'=>$this->data['id']],$this->data);
                if($res){
                    return array('code'=>2000,'msg'=>'success');
                }else{
                    return array('code'=>500,'msg'=>'fail');
                }
            }
            
        }else{

            $language = getDatas('language',['*'],['id'=>$this->data['id']])[0];
            // return $language;
            return view('open',['title'=>'编辑语言类型','id'=>$this->data['id'],'request'=>'edit','language'=>$language]);
            
            
        }
    }

    public function changeStatus(){
        // return $this->data;
        if($this->data['id']){
            $res = updateData('language',['id'=>$this->data['id']],['hide'=>$this->data['status']]);
            if($res){
                return array('code'=>2000,'msg'=>'success');
            }else{
                return array('code'=>500,'msg'=>'fail');
            }
        }else{
            return array('code'=>500,'msg'=>'miss id');
        }
    }

    public function delete(){
        if($this->data['id']){
            $res = deleteData('language',['id'=>$this->data['id']]);
            if($res){
                return array('code'=>2000,'msg'=>'success');
            }else{
                return array('code'=>500,'msg'=>'fail');
            }
        }else{
            return array('code'=>500,'msg'=>'miss id');
        }
    }


}